package lipengfei.botaiframework.security.jwt;

import java.util.ArrayList;
import java.util.List;

import lipengfei.botaiframework.security.jwt.validator.ValidatorModel;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lipengfei.botaiframework.security.jwt.authentication.JWTAuthenticationManager;
import lipengfei.botaiframework.security.jwt.authentication.impl.AssignTokenAuthenticationManager;
import lipengfei.botaiframework.security.jwt.handler.TokenExpiredHandler;
import lipengfei.botaiframework.security.jwt.handler.TokenSuccessHandler;
import lipengfei.botaiframework.security.jwt.handler.impl.InvalidTokenHandler;
import lipengfei.botaiframework.security.jwt.handler.impl.SuccessTokenHandler;
import lipengfei.botaiframework.security.jwt.handler.impl.RefreshTokenHandler;
import lipengfei.botaiframework.security.jwt.handler.impl.ExpireTokenHandler;
import lipengfei.botaiframework.security.jwt.validator.Validator;
import lipengfei.botaiframework.security.jwt.validator.impl.ExpiredValidator;
import lipengfei.botaiframework.security.jwt.validator.impl.NeedRefreshValidator;
import lipengfei.botaiframework.security.jwt.validator.impl.SignatureValidator;

@Configuration
public class JWTAutoConfiguration {

	
     @ConditionalOnMissingBean(TokenSuccessHandler.class)
     @Bean
     TokenSuccessHandler tokenSuccessHandler(){
        return new SuccessTokenHandler();
     }

     @ConditionalOnMissingBean(TokenExpiredHandler.class)
	 @Bean
	 TokenExpiredHandler expiredHandler(){
	     return new ExpireTokenHandler();
	 }

     @ConditionalOnMissingBean(lipengfei.botaiframework.security.jwt.handler.TokenRefreshHandler.class)
	 @Bean
	 lipengfei.botaiframework.security.jwt.handler.TokenRefreshHandler refreshHandler(){
	     return new RefreshTokenHandler();
	 }

     @ConditionalOnMissingBean(lipengfei.botaiframework.security.jwt.handler.TokenInvalidHandler.class)
	 @Bean
     lipengfei.botaiframework.security.jwt.handler.TokenInvalidHandler tokenInvalidHandler(){
	     return new InvalidTokenHandler();
	 }

	@ConditionalOnMissingBean(JWTAuthenticationManager.class)
	@Bean
	JWTAuthenticationManager validationManager(){
		List<Validator<ValidatorModel>> validators = new ArrayList<Validator<ValidatorModel>>();
		validators.add(new SignatureValidator());
		validators.add(new NeedRefreshValidator());
		validators.add(new ExpiredValidator());
		return new AssignTokenAuthenticationManager(validators);
	}


	    
	    
	    

	   
    
	 
	
	
	    
	    
}
